package club.banyuan.mbm;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class BillService {
    private int idCount = 1;
    private List<Bill> billList = new ArrayList<>();

    // 增加
    public void addBill(Bill bill) {
        bill.setId(idCount++);
        billList.add(bill);
        save();
    }

    // 根据id删除
    public void deleteBill(int id) {
        billList.removeIf(bill -> bill.getId() == id);
        save();
    }

    // 根据id查询
    public Bill getBill(int id) {
        for (Bill bill : billList) {
            if (bill.getId() == id) {
                return bill;
            }
        }
        return null;
    }

    // 更改
    public void updateBill(Bill bill) {
        Bill old = getBill(bill.getId());
        if (old != null) {
            old.setProviderId(bill.getProviderId());
            old.setIsPay(bill.getIsPay());
            old.setMoney(bill.getMoney());
            old.setProduct(bill.getProduct());
            save();
        }
    }

    // 序列化
    private void save() {
        String path = getPath();
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
            writer.write(JSONObject.toJSONString(this));
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 反序列化
    public static BillService load() {
        String path = getPath();
        File file = new File(path);
        if (!file.exists()) {
            File newFile = new File(file.getParent());
            newFile.mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new BillService();
        } else {
            try (InputStream inputStream = new FileInputStream(file)) {
                return JSONObject.parseObject(inputStream, StandardCharsets.UTF_8, BillService.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 获取配置文件里的路径
    private static String getPath() {
        Properties properties = new Properties();
        try {
            properties.load(UserService.class.getResourceAsStream("/app.properties"));
        } catch (IOException e) {
            System.out.println("读取配置文件失败");
            System.exit(-1);
        }
        return properties.getProperty("bill.store.path");
    }

    public int getIdCount() {
        return idCount;
    }

    public void setIdCount(int idCount) {
        this.idCount = idCount;
    }

    public List<Bill> getBillList() {
        return billList;
    }

    public void setBillList(List<Bill> billList) {
        this.billList = billList;
    }

    public List<Bill> getBillList(Bill bill) {
        List<Bill> rlt = new ArrayList<>();
        if (bill.getProduct() == null && !bill.getIsPay().equals("-1")) {
            for (Bill one : billList) {
                if (one.getIsPay().contains(bill.getIsPay())) {
                    rlt.add(one);
                }
            }
        } else if (bill.getProduct() != null && bill.getIsPay().equals("-1")) {
            for (Bill one : billList) {
                if (one.getProduct().contains(bill.getProduct())) {
                    rlt.add(one);
                }
            }
        } else if (bill.getProduct() == null && bill.getIsPay().equals("-1")) {
            rlt = billList;
        } else {
            for (Bill one : billList) {
                if (one.getProduct().contains(bill.getProduct()) && one.getIsPay().contains(bill.getIsPay())) {
                    rlt.add(one);
                }
            }
        }
        return rlt;
    }
}
